﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Ree7.TileToolkit.TileTemplates;

namespace Ree7.TileToolkit.Sample
{
	public partial class MainPage : PhoneApplicationPage
	{
		CustomTile myCustomTile1, myCustomTile2;

		// Constructor
		public MainPage()
		{
			InitializeComponent();

			Loaded += new RoutedEventHandler(MainPage_Loaded);
		}

		void MainPage_Loaded(object sender, RoutedEventArgs e)
		{
			CreateSampleTiles();
		}

		private void CreateSampleTiles()
		{
			// Example use of the TileToolkit
			myCustomTile1 = CustomTileFactory.GetTemplatedTile("SampleTile",
				// You can create your own templates, this one is design to be a pixel perfect copy of WP7's native tiles with counters
				new MediumCountTileTemplate()
				{
					// You can set any kind of brush as the background (Image, Gradient, SolidColor)
					// or just do not set the property to leave it transparent (and use the phone theme color)
					// You can also tweak the foreground color, which will affect text items (count, app name)
					// but to follow the WP guidelines, you'd better stick with white (default).
					Background = null,
					// For the icon you have to use a BitmapImage (from your app content, resources, from a remote url, whatever...)
					Icon = new BitmapImage()
					{
						CreateOptions = BitmapCreateOptions.IgnoreImageCache,
						UriSource = new Uri("SampleTileIcon.png", UriKind.Relative)
					},
					// The count index displayed on the tile
					Count = "Hi",
					// The app name displayed on the tile
					Title = "Sample tile"
				},
				TileSize.Medium);

			// Transform the system tile size (336x336) to its regular display size on a WVGA Windows Phone (210x210)
			myCustomTile1.RenderTransform = CustomTileFactory.GetUITransform();

			// Display the tile in the UI (totally optional)
			SimpleTileContainer.Children.Add(myCustomTile1);

			// A second example, without verbose this time because... well you already got it !
			myCustomTile2 = CustomTileFactory.GetTemplatedTile("SampleTile2",
				new MediumCountTileTemplate()
				{
					Background = new ImageBrush()
					{
						ImageSource = new BitmapImage()
						{
							CreateOptions = BitmapCreateOptions.IgnoreImageCache,
							UriSource = new Uri("SampleTileBackground.png", UriKind.Relative)
						}
					},
					Icon = new BitmapImage()
					{
						CreateOptions = BitmapCreateOptions.IgnoreImageCache,
						UriSource = new Uri("SampleTileIcon2.png", UriKind.Relative)
					},
					Count = "10°",
					Title = "Paris, FR"
				},
				TileSize.Medium);

			myCustomTile2.RenderTransform = CustomTileFactory.GetUITransform();
			ComplexTileContainer.Children.Add(myCustomTile2);
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			// Remove the tile if it already exists or the creation will miserably fail
			ShellTile existingTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("SampleTile"));
			if (existingTile != null) existingTile.Delete();

			// For this example we will use customTile1 created previously as the medium sized tile
			// and create the small and wide tiles from code
			// Be sure to specify different tile names for each size as the name is used for the output
			// PNG file name.
			CustomTile smallTile = CustomTileFactory.GetTemplatedTile("SampleSmallTile", new SmallIconAndLabelTileTemplate()
			{
				Background = null, 
				Icon = new BitmapImage()
				{
					CreateOptions = BitmapCreateOptions.None,
					UriSource = new Uri("SampleSmallTileIcon.png", UriKind.Relative)
				},
				Label = "Hi"
			},
			TileSize.Small);
			smallTile.RenderTransform = CustomTileFactory.GetUITransform();
			SimpleTileContainer.Children.Add(smallTile);

			CustomTile wideTile = CustomTileFactory.GetTemplatedTile("SampleWideTile", new LargeContentAndLogoTileTemplate()
			{
				Background = new ImageBrush()
				{
					ImageSource = new BitmapImage()
					{
						CreateOptions = BitmapCreateOptions.None,
						UriSource = new Uri("SampleWideTileBackground.png", UriKind.Relative)
					},
					Stretch = System.Windows.Media.Stretch.UniformToFill
				},
				Logo = new BitmapImage()
				{
					CreateOptions = BitmapCreateOptions.None,
					UriSource = new Uri("SampleSmallTileIcon.png", UriKind.Relative)
				},
				Title = "Sample tile",
				Text = "Anonymous says : 'Hi'. This text can span across multiple lines."
			},
			TileSize.Wide);
			wideTile.RenderTransform = CustomTileFactory.GetUITransform();
			SimpleTileContainer.Children.Add(wideTile);

			// Get a StandardTileData object from the tile helper. The Helper will render the XAML tiles as PNG in the
			// isolated storage and return a FlipTileData object with the correct URI.
			// Only the medium tile is mandatory.
			// The returned object is a standard Windows Phone FlipTileData and you can customize it to overlay text (see the 
			// next sample).
			StandardTileData std = CustomTileHelper.GetTileData(myCustomTile1, wideTile, smallTile);
			ShellTile.Create(new Uri("/MainPage.xaml?src=SampleTile", UriKind.Relative), std, false);
		}

		private void Button2_Click(object sender, RoutedEventArgs e)
		{
			// Remove the tile if it already exists or the creation will miserably fail
			ShellTile existingTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("SampleTile2"));
			if (existingTile != null) existingTile.Delete();

			// Example of further customization of the Standard page data
			FlipTileData std = CustomTileHelper.GetTileData(myCustomTile2) as FlipTileData;
			std.BackContent = "Cloudy weather, no rain.";

			ShellTile.Create(new Uri("/MainPage.xaml?src=SampleTile2", UriKind.Relative), std, false);
		}

		private void Button3_Click(object sender, RoutedEventArgs e)
		{
			// Test and demo for creating a tile that has not been added in the visual tree
			CustomTile dynamicTile = CustomTileFactory.GetTemplatedTile("SampleTile3", new MediumCountTileTemplate()
			{
				Background = null,
				Icon = null,
				Count = ":D",
				Title = "Tile w/o visual"
			},
				TileSize.Medium);

			// Remove the tile if it already exists or the creation will miserably fail
			ShellTile existingTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("SampleTile3"));
			if (existingTile != null) existingTile.Delete();
			ShellTile.Create(new Uri("/MainPage.xaml?src=SampleTile3", UriKind.Relative), dynamicTile.GetShellTileData(), false);
		}
	}
}